package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.BorderFactory;
import javax.swing.JPanel;



public class GridCellPanel extends JPanel
{
	private static final long serialVersionUID = 1L;
    private Font smallFont;
    private Font defaultFont;
    private int cellValue;
    private Color cellColor;
    
    public GridCellPanel()
    {
    }
    
    public GridCellPanel(int cellValue, Color col)
    {
        defaultFont = new Font(getFont().getName(), Font.BOLD, 14);
        smallFont = new Font(defaultFont.getName(), Font.PLAIN, 10);
        
        setBorder(BorderFactory.createLineBorder(Color.black, 1));
        setBackground(Color.WHITE);
        setCellColor(col);
        setCellValue(cellValue);
    }
    
    public int getCellValue()
    {
    	return this.cellValue;
    }
    
    public void setCellValue(int cValue)
    {
		// TODO Auto-generated method stub
		this.cellValue = cValue;
		repaint();
	}

    public Color getCellColor()
    {
    	return this.cellColor;
    }
    
    public void setCellColor(Color col)
    {
    	this.cellColor = col;
    }
    
    @Override
    public void paint(Graphics g)
    {
    	if (this.cellValue != 0)
    	{
	        Graphics2D g2 = (Graphics2D)g;
	        g2.setColor(Color.GRAY);
	        g2.fillRect(0, 0, getWidth(), getHeight());
	        g2.setColor(Color.BLACK);
	        g2.drawRect(0, 0, getWidth(), getHeight());
	        g2.setColor(this.cellColor);
	        g2.setFont(defaultFont);
	        
	        String cellValue = Integer.toString(this.cellValue);
	        FontMetrics fm = g2.getFontMetrics();
	        Rectangle2D rect = fm.getStringBounds(cellValue, g2);
	        g2.drawString(cellValue, (int) ((getWidth() / 2f) - (rect.getWidth() / 2f)), (int) ((getHeight() / 2f) + (rect.getHeight() / 3f)));
    	}
    }
}

